package com.example.demo6.ui.login;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.example.demo6.Account;
import com.example.demo6.R;
import com.example.demo6.SQLite.MyDatabase;
import com.example.demo6.SQLite.User;

public class loginFragment extends Fragment {

    EditText editTextUsername, editTextPassword;
    Button signinBtn, exitBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);

        editTextUsername = view.findViewById(R.id.editUserName);
        editTextPassword = view.findViewById(R.id.editUserPassword);
        signinBtn = view.findViewById(R.id.signinbtn1);
        exitBtn = view.findViewById(R.id.exitbtn1);
        TextView tologin = view.findViewById(R.id.tologin);
        tologin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment_activity_main);
                navController.navigate(R.id.registFragment);
            }
        });
        signinBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = editTextUsername.getText().toString().trim();
                String password = editTextPassword.getText().toString().trim();

                if (validateInput(username, password)) {
                    new LoginTask(username, password).execute(); // 启动异步任务
                } else {
                    Toast.makeText(requireContext(), "请填写用户名和密码", Toast.LENGTH_SHORT).show();
                }
            }
        });

        exitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理取消按钮的点击事件，比如关闭当前Fragment
            }
        });

        return view;
    }

    private class LoginTask extends AsyncTask<Void, Void, User> {
        private String username, password;

        public LoginTask(String username, String password) {
            this.username = username;
            this.password = password;
        }

        @Override
        protected User doInBackground(Void... voids) {
            return MyDatabase.getInstance(requireContext()).userDao().getUserByUsernameAndPassword(username, password);
        }

        @Override
        protected void onPostExecute(User user) {
            if (user != null) {
                Toast.makeText(requireContext(), "登录成功", Toast.LENGTH_SHORT).show();
                NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment_activity_main);
                Bundle bundle=new Bundle();
                bundle.putString("username", user.getUsername());//传参
                Account.getInstance().setCurrentUsername(user.getUsername());
                navController.navigate(R.id.centerFragment,bundle); // 导航到登录成功后的目的地
            } else {
                Toast.makeText(requireContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private boolean validateInput(String username, String password) {
        return !TextUtils.isEmpty(username) && !TextUtils.isEmpty(password);
    }
}
